package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import com.itheima.reggie.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.ListResourceBundle;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 分类管理(添加):
     *      新增菜品分类 / 新增套餐分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Category category){
        log.info("套餐分类: {}",category);

        Long employee = (Long) request.getSession().getAttribute("employee");
        BaseContext.setCurrentId(employee);

        categoryService.save(category);     // 执行添加操作
        return R.success("新增分类成功!");
    }


    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        log.info("查询页码: {}","查询条数: {}",page,pageSize);

        // 分页构造器(将查询页码和条数传入)
        Page<Category> page1 =new Page<>(page, pageSize);
        // 条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        // 设置排序条件
        queryWrapper.orderByAsc(Category::getSort);

        // 分页查询
        categoryService.page(page1,queryWrapper);

        return R.success(page1);
    }

    /**
     * 分类修改
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Category category){
        log.info("修改参数: {}",category);

        Long employee = (Long) request.getSession().getAttribute("employee");
        BaseContext.setCurrentId(employee);

        // 执行修改
        categoryService.updateById(category);

        return R.success("修改成功!");
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long id){
        log.info("删除分类的id: {}",id);

        // 查询Dish表中该id是否有信息
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId, id);
        int count = dishService.count(queryWrapper);
        if (count != 0){
            log.info("关联菜品数量:{}",count);
            // 在common包下创建一个自定义异常类,并触发这个异常
            throw new CustomException("当前分类下关联有菜品,无法删除!");
        }

        // 查询Setmeal表中该id是否有信息
        LambdaQueryWrapper<Setmeal> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(queryWrapper1);
        if (count1 != 0){
            log.info("关联套餐数量:{}",count1);
            throw new CustomException("当前分类下关联有套餐,无法删除!");
        }

        // 删除
        categoryService.removeById(id);

        return R.success("删除成功!");
    }

    /**
     * 菜品分类查询
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        log.info("category接收数据:{}",category.toString());    // 前端传来的是type是1,表示查询菜品分类

        // 条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件
        queryWrapper.eq(category.getType() != null,Category::getType,category.getType());
        // 添加排序条件
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        // 查询
        List<Category> list = categoryService.list(queryWrapper);

        return R.success(list);
    }
}
